/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2020. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

#ifndef _AC780X_RTC_REG_H
#define _AC780X_RTC_REG_H

/*!
 * @file ac780x_rtc_reg.h
 *
 * @brief RTC access register inline function definition.
 *
 */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* ===========================================  Includes  =========================================== */
#include "ac780x_rtc.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Get RTC counter enable value.
 *
 * @param[in] base: RTC base pointer
 * @return State of the counter enable bit
 *         - true if the counter is enabled
 *         - false if the counter is disabled
 */
__STATIC_INLINE BOOL_Type RTC_GetTimeCounterEnable(const RTC_Type * const base)
{
    return (BOOL_Type)(((base->SR & RTC_SR_RTCEN_Msk) >> RTC_SR_RTCEN_Pos) != 0U);
}

/*!
 * @brief get RTC control and state register.
 *
 * @return control and status register value
 */
__STATIC_INLINE uint32_t RTC_GetCtrlStatusReg(void)
{
    return (RTC->SR);
}

/*!
 * @brief set RTC clock source.
 *
 * @param[in] clkSource:
 *    0: RTC_CLOCK_APB
 *    1: RTC_CLOCK_LSI
 *    2: RTC_CLOCK_HSE
 *    3: RTC_CLOCK_LSE_PASSIVE
 *    7: RTC_CLOCK_LSE_ACTIVE
 * @return none
 */
__STATIC_INLINE void RTC_SetClkSource(RTC_ClockSourceType clkSource)
{
    uint32_t tmp = RTC->CR & (~RTC_CR_RTCCLKS_Msk);

    tmp |= (((uint32_t)clkSource) << RTC_CR_RTCCLKS_Pos) & RTC_CR_RTCCLKS_Msk;
    RTC->CR = tmp;
}

/*!
 * @brief set RTC prescaler value.
 *
 * @param[in] psrValue: 20bit RTC prescaler value
 * @return none
 */
__STATIC_INLINE void RTC_SetPrescaler(uint32_t psrValue)
{
    RTC->PSR = psrValue;
}

/*!
* @brief get RTC prescaler value
*
* @param[in] none
* @return RTC prescaler value
*/
__STATIC_INLINE uint32_t RTC_GetPrescaler(void)
{
    return (RTC->PSR);
}

/*!
 * @brief set RTC timer alarm register value.
 *
 * @param[in] 32 bit RTC alarm value
 * @return none
 */
__STATIC_INLINE void RTC_SetTimeAlarmRegister(uint32_t alarmValue)
{
    RTC->TAR = alarmValue;
}

/*!
 * @brief get RTC timer alarm register value.
 *
 * @param[in] none
 * @return RTC alarm value
 */
__STATIC_INLINE uint32_t RTC_GetTimerAlarmRegister(void)
{
    return (RTC->TAR);
}

/*!
 * @brief get RTC prescaler value.
 *
 * @param[in] none
 * @return RTC prescaler cnt value
 */
__STATIC_INLINE uint32_t RTC_GetPrescalerCnt(void)
{
    return (RTC->PSC);
}

/*!
 * @brief get RTC cnt value.
 *
 * @param[in] none
 * @return RTC cnt value
 */
__STATIC_INLINE uint32_t RTC_GetTimeCntRegister(void)
{
    return (RTC->TC);
}


/*!
 * @brief Set RTC time counter value.
 *
 * @param[in] base: The RTC base pointer
 * @param[in] timeCnt: 32-bit RTC time counter value
 * @return the result of operation
 *         - STATUS_SUCCESS: operation was successful
 *         - STATUS_ERROR: if setting value error because timer is enabled
 */
__STATIC_INLINE BOOL_Type RTC_SetTimeCntRegister(uint32_t timeCnt)
{
    BOOL_Type status;
    if (RTC_GetTimeCounterEnable(RTC))
    {
        status = FALSE;
    }
    else
    {
        RTC->TC = timeCnt;
        status = TRUE;
    }

    return status;
}


/*!
 * @brief enable RTC alarm interrupt.
 *
 * @param[in] enable: enable RTC alarm interrupt
 * @return none
 */
__STATIC_INLINE void RTC_SetTAIE(ACTION_Type enable)
{
    uint32_t tmp = RTC->CR;

    tmp &= ~RTC_CR_TAIE_Msk;
    if (ENABLE == enable)
    {
        tmp |= RTC_CR_TAIE_Msk;
    }
    RTC->CR = tmp;
}

/*!
 * @brief enable RTC overflow interrupt.
 *
 * @param[in] enable: enable RTC overflow interrupt
 * @return none
 */
__STATIC_INLINE void RTC_SetTOIE(ACTION_Type enable)
{
    uint32_t tmp = RTC->CR;

    tmp &= ~RTC_CR_TOIE_Msk;
    if (ENABLE == enable)
    {
        tmp |= RTC_CR_TOIE_Msk;
    }
    RTC->CR = tmp;
}


/*!
 * @brief enable RTC prescaler interrupt.
 *
 * @param[in] enable: enable RTC prescaler interrupt
 * @return none
 */
__STATIC_INLINE void RTC_SetPOIE(ACTION_Type enable)
{
    uint32_t tmp = RTC->CR;

    tmp &= ~RTC_CR_POIE_Msk;
    if (ENABLE == enable)
    {
        tmp |= RTC_CR_POIE_Msk;
    }
    RTC->CR = tmp;
}

/*!
 * @brief enable RTC output.
 *
 * @param[in] enable: enable RTC output
 * @return none
 */
__STATIC_INLINE void RTC_SetRTCOutput(ACTION_Type enable)
{
    uint32_t tmp = RTC->CR;

    tmp &= ~RTC_CR_COE_Msk;
    if (ENABLE == enable)
    {
        tmp |= RTC_CR_COE_Msk;
    }
    RTC->CR = tmp;
}

/*!
 * @brief Enable/disable RTC module.
 *
 * This function enable the RTC module.
 *
 * @param[in] state: enabling state
                 - ENABLE
                 - DISABLE
 * @return none
 */
__STATIC_INLINE void RTC_Enable(ACTION_Type state)
{
    MODIFY_REG32(RTC->SR, RTC_SR_RTCEN_Msk, RTC_SR_RTCEN_Pos, state);
}

/*!
 * @brief get rtc alarm interrupt flag.
 *
 * @param[in] none
 * @return rtc alarm interrupt flag
 */
__STATIC_INLINE uint32_t RTC_GetTAF(void)
{
    return (RTC->SR & RTC_SR_TAF_Msk);
}

/*!
 * @brief clear rtc alarm interrupt flag.
 *
 * @param[in] none
 * @return none
 */
__STATIC_INLINE void RTC_ClearTAF(void)
{
    uint32_t tmp = 0x1;

    RTC->TAR = tmp;
}

/*!
 * @brief get rtc overflow interrupt flag.
 *
 * @param[in] none
 * @return rtc overflow interrupt flag
 */
__STATIC_INLINE uint32_t RTC_GetTOF(void)
{
    return (RTC->SR & RTC_SR_TOF_Msk);
}

/*!
 * @brief clear rtc overflow interrupt flag.
 *
 * @param[in] none
 * @return none
 */
__STATIC_INLINE void RTC_ClearTOF(void)
{
    uint32_t tmp = 0x1;
    RTC_Enable(DISABLE);

    RTC->TC = tmp;
}

/*!
 * @brief get rtc prescaler interrupt flag.
 *
 * @param[in] none
 * @return rtc prescaler interrupt flag
 */
__STATIC_INLINE uint32_t RTC_GetPOF(void)
{
    return (RTC->SR & RTC_SR_POF_Msk);
}

/*!
 * @brief clear rtc prescaler interrupt flag.
 *
 * @param[in] none
 * @return none
 */
__STATIC_INLINE void RTC_ClearPOF(void)
{
    uint32_t tmp = RTC->SR & (~RTC_SR_POF_Msk);

    tmp |= RTC_SR_POF_Msk;
    RTC->SR = tmp;
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _AC780X_RTC_REG_H */

/* =============================================  EOF  ============================================== */
